const {app, BrowserWindow, Menu, MenuItem} = require('electron');
const path = require('path');

//Create native application menus and context menus.
//https://www.electronjs.org/docs/latest/api/menu

app.on('ready', () => {
  let win = new BrowserWindow({
    //https://www.electronjs.org/docs/latest/api/browser-window#new-browserwindowoptions
    width: 802,
    height: 550,

    webPreferences: {
      nodeIntegration: true,
      // contextIsolation: true,
      // enableRemoteModule: false,
      // devTools: true
    }
  });

  //template (MenuItemConstructorOptions | MenuItem)[]
  const menu = new Menu();
  const itemArr = [
    {
      label: '打开'
      ,id:'open'
      , click: () => {
        console.log('打开');
      }
    }, {
      label: '重命名'
      ,id:'rename'
      ,click: () => {
        console.log('重命名');
      }
    },
    {
      label: '删除'
      ,id:'delete'
      ,click: () => {
        console.log('删除');
      }
    }
  ]
  itemArr.forEach(item => {
    menu.append(new MenuItem(item))
  });

  menu.insert(1/*位置从1开始, 1的话, 就是插到第一项后面, 也就是作为整个菜单的第二项*/, new MenuItem({
    label: '新插入的item'
    , id: 'newItem'
    , click: () => {
      console.log('新插入的item');
    }
  }));

  menu.popup({
    x:100,
    y:100
  });

  win.loadFile(path.resolve(__dirname, 'index.html'))
});

